import java.util.List;
import java.util.Scanner;

public class ScheduleService {
    private Scanner scanner;
    private ScheduleDAO scheduleDAO;
    private MovieDAO movieDAO;

    public ScheduleService(Scanner scanner) {
        this.scanner = scanner;
        this.scheduleDAO = new ScheduleDAO();
        this.movieDAO = new MovieDAO();
    }

    // 查看所有场次
    public void listAllSchedules() {
        List<Schedule> schedules = scheduleDAO.getAllSchedules();
        System.out.println("\n=== 场次列表 ===");
        System.out.println("ID\t电影ID\t放映厅\t日期\t\t时间\t剩余座位");
        System.out.println("------------------------------------------------");
        for (Schedule schedule : schedules) {
            System.out.println(String.format("%s\t%s\t%s\t%s\t%s\t%d",
                    schedule.getId(), schedule.getMovieId(), schedule.getCinemaHall(),
                    schedule.getDate(), schedule.getTime(), schedule.getRemainingSeats()));
        }
    }

    // 根据电影ID查看场次
    public void listSchedulesByMovieId(String movieId) {
        List<Schedule> schedules = scheduleDAO.findSchedulesByMovieId(movieId);
        if (schedules.isEmpty()) {
            System.out.println("该电影暂无排片！");
            return;
        }

        System.out.println("\n=== 电影场次列表 ===");
        System.out.println("ID\t放映厅\t日期\t\t时间\t剩余座位");
        System.out.println("----------------------------------------");
        for (Schedule schedule : schedules) {
            System.out.println(String.format("%s\t%s\t%s\t%s\t%d",
                    schedule.getId(), schedule.getCinemaHall(),
                    schedule.getDate(), schedule.getTime(), schedule.getRemainingSeats()));
        }
    }

    // 根据ID查找场次
    public Schedule findScheduleById(String id) {
        return scheduleDAO.findScheduleById(id);
    }

    // 添加场次
    public boolean addSchedule() {
        System.out.print("请输入场次ID: ");
        String id = scanner.nextLine();

        // 检查场次ID是否已存在
        if (scheduleDAO.findScheduleById(id) != null) {
            System.out.println("场次ID已存在！");
            return false;
        }

        System.out.print("请输入电影ID: ");
        String movieId = scanner.nextLine();

        // 检查电影是否存在
        if (movieDAO.findMovieById(movieId) == null) {
            System.out.println("电影不存在！");
            return false;
        }

        System.out.print("请输入放映厅: ");
        String cinemaHall = scanner.nextLine();
        System.out.print("请输入日期(yyyy-mm-dd): ");
        String date = scanner.nextLine();
        System.out.print("请输入时间(hh:mm): ");
        String time = scanner.nextLine();
        System.out.print("请输入座位数: ");
        int remainingSeats = Integer.parseInt(scanner.nextLine());

        Schedule schedule = new Schedule(id, movieId, cinemaHall, date, time, remainingSeats);
        boolean result = scheduleDAO.addSchedule(schedule);
        if (result) {
            System.out.println("场次添加成功！");
        } else {
            System.out.println("场次添加失败！");
        }
        return result;
    }

    // 更新场次信息
    public boolean updateSchedule() {
        System.out.print("请输入要更新的场次ID: ");
        String id = scanner.nextLine();

        Schedule schedule = scheduleDAO.findScheduleById(id);
        if (schedule == null) {
            System.out.println("场次不存在！");
            return false;
        }

        System.out.print("请输入新的电影ID(" + schedule.getMovieId() + "): ");
        String movieId = scanner.nextLine();
        if (!movieId.isEmpty()) {
            // 检查电影是否存在
            if (movieDAO.findMovieById(movieId) == null) {
                System.out.println("电影不存在！");
                return false;
            }
            schedule.setMovieId(movieId);
        }

        System.out.print("请输入新的放映厅(" + schedule.getCinemaHall() + "): ");
        String cinemaHall = scanner.nextLine();
        if (!cinemaHall.isEmpty()) {
            schedule.setCinemaHall(cinemaHall);
        }

        System.out.print("请输入新的日期(" + schedule.getDate() + "): ");
        String date = scanner.nextLine();
        if (!date.isEmpty()) {
            schedule.setDate(date);
        }

        System.out.print("请输入新的时间(" + schedule.getTime() + "): ");
        String time = scanner.nextLine();
        if (!time.isEmpty()) {
            schedule.setTime(time);
        }

        System.out.print("请输入新的座位数(" + schedule.getRemainingSeats() + "): ");
        String seatsStr = scanner.nextLine();
        if (!seatsStr.isEmpty()) {
            schedule.setRemainingSeats(Integer.parseInt(seatsStr));
        }

        boolean result = scheduleDAO.updateSchedule(schedule);
        if (result) {
            System.out.println("场次更新成功！");
        } else {
            System.out.println("场次更新失败！");
        }
        return result;
    }

    // 删除场次
    public boolean deleteSchedule() {
        System.out.print("请输入要删除的场次ID: ");
        String id = scanner.nextLine();

        Schedule schedule = scheduleDAO.findScheduleById(id);
        if (schedule == null) {
            System.out.println("场次不存在！");
            return false;
        }

        boolean result = scheduleDAO.deleteSchedule(id);
        if (result) {
            System.out.println("场次删除成功！");
        } else {
            System.out.println("场次删除失败！");
        }
        return result;
    }

    // 更新剩余座位数
    public boolean updateRemainingSeats(String scheduleId, int remainingSeats) {
        return scheduleDAO.updateRemainingSeats(scheduleId, remainingSeats);
    }
}
